package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class LimitRatioServiceImpl implements LimitRatioService{
    protected static final Logger LOG = LoggerFactory.getLogger(LimitRatioServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private LimitRatioMapper limitRatioMapper;

    @Override
    public LimitRatio create(LimitRatio item) {
         limitRatioMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return limitRatioMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(LimitRatio item) {
        return limitRatioMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public LimitRatio get(Integer id) {
        return limitRatioMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<LimitRatio> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,LimitRatio item){
       LimitRatioExample  example = new LimitRatioExample();
       LimitRatioExample.Criteria criteria = example.createCriteria();
       if(item.getLevel() != null ){
            criteria.andLevelEqualTo(item.getLevel());
        }
        if(item.getRatio() != null ){
            criteria.andRatioEqualTo(item.getRatio());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<LimitRatio> list = this.limitRatioMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<LimitRatio> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,LimitRatio item){
       LimitRatioExample  example = new LimitRatioExample();
       LimitRatioExample.Criteria criteria = example.createCriteria();
       if(item.getLevel() != null ){
            criteria.andLevelEqualTo(item.getLevel());
        }
        if(item.getRatio() != null ){
            criteria.andRatioEqualTo(item.getRatio());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<LimitRatio> list = this.limitRatioMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,LimitRatio item){
        LimitRatioExample  example = new LimitRatioExample();
        LimitRatioExample.Criteria criteria = example.createCriteria();
        if(item.getLevel() != null ){
            criteria.andLevelEqualTo(item.getLevel());
        }
        if(item.getRatio() != null ){
            criteria.andRatioEqualTo(item.getRatio());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<LimitRatio> list = this.limitRatioMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(limitRatio -> JSONObject.parseObject(JSONObject.toJSONString(limitRatio))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

}
